﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtils.Utils.SpecialUtils.PicBed.Sina;

using System;
using System.Collections.Generic;

namespace SinaImgUpload.SainTool.LocalTool
{
    internal class UploadHtml
    {
        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="FloderName"></param>
        public void UploadFloder(string FloderName)
        {
            List<string> Templist = FloderNameHelper.GetFullFileNameList(FloderName);
            int Completed = 0;
            foreach (string SingleFlie in Templist)
            {
                new PrintLog().PercentLog(Templist.Count, ++Completed);
                UploadSingle(SingleFlie);
            }
        }

        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="FileName"></param>
        public void UploadSingle(string FileName)
        {
            String Content = FileHelper.ReadAllText(FileName);

            if (Content == "")
                return;
            List<string> ImgList = HtmlHelper.GetImgSrcVlaueList(Content, "//img");
            foreach (String SingleImg in ImgList)
            {
                //判断已处理情况
                if (SingleImg.IndexOf("sinaimg.cn") > 0)
                    break;
                string SinaImgLink = SinaImgUploadTool.UploadImgLink(SingleImg);
                // new SinaImgUpload.SinaUpload.SinaImg().UploadImgUrl(SingleImg)
                if (SinaImgLink != "")
                    Content = Content.Replace(SingleImg, SinaImgLink);
            }
            Console.WriteLine("处理{0}完成", FileNameHelper.GetFileNameWithoutExtension(FileName));
            if (Content.IndexOf("sinaimg.cn") > 0)
                FileHelper.WriteText(FileNameHelper.CreateFileNameWithChildFloder(FileName, "图片上传完成"), Content);
            else
            {
                string NewFilePath = FileNameHelper.CreateFileNameWithChildFloder(FileName, "图片上传失败");
                FileHelper.FileMove(FileName, NewFilePath, true);
            }
            FileHelper.FileDelete(FileName);
        }
    }
}